package com.jfree.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author wangjian
 * @date 2019/8/23 11:22
 */
@Configuration
public class ElasticSearchClientConfig {


    @Value("${es.ip}")
    private String firstIp;
    @Value("${es.port}")
    private String  firstPort;
    @Bean(name = "elasticSearchClient")
    @Primary
     RestHighLevelClient getRestHighLevelClient() throws IOException {
        RestHighLevelClient client = null;
        try {

            client = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost(InetAddress.getByName(firstIp),Integer.parseInt(firstPort),"http")));

        } catch (UnknownHostException e) {
            e.printStackTrace();
            client.close();
        }

        return client;
}
}
//    @Bean
//    public RestHighLevelClient client() {
//        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
//        // 异步httpclient连接延时配置
//        builder.setRequestConfigCallback(new RequestConfigCallback() {
//            @Override
//            public Builder customizeRequestConfig(Builder requestConfigBuilder) {
//                requestConfigBuilder.setConnectTimeout(connectTimeOut);
//                requestConfigBuilder.setSocketTimeout(socketTimeOut);
//                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
//                return requestConfigBuilder;
//            }
//        });
//        // 异步httpclient连接数配置
//        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
//            @Override
//            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                httpClientBuilder.setMaxConnTotal(maxConnectNum);
//                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
//                return httpClientBuilder;
//            }
//        });
//        RestHighLevelClient client = new RestHighLevelClient(builder);
//        return client;
//    }
